package com.jy.datapipeline.export.service.impl;

import com.jy.datapipeline.export.dao.repo.DataDictionaryRepo;
import com.jy.datapipeline.export.entity.DataDictionary;
import com.jy.datapipeline.export.service.DataDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class DataDictionaryServiceImpl implements DataDictionaryService {
    @Autowired
    private DataDictionaryRepo dataDictionaryDao;
    
    private static final List<DataDictionary> CACHE = new ArrayList<>();
    
    @Override
    public synchronized List<DataDictionary> findByType(String type) {
        if (CACHE.isEmpty()) {
            List<DataDictionary> byType = dataDictionaryDao.findByType(type);
            CACHE.addAll(byType);
            return byType;
        }
        return CACHE;
    }

    @Override
    public String findById(Integer cityCodeId) {
        Optional<DataDictionary> optional = dataDictionaryDao.findById(cityCodeId);
        return optional.map(DataDictionary::getLabelKey).orElse(null);
    }
}
